package com.damage.supplier.service.impl;

import com.damage.common.entity.SupplyGood;
import com.damage.common.exception.RelateException;
import com.damage.supplier.mapper.ISupplyGoodMapper;
import com.damage.supplier.service.ISupplyGoodService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SupplyGoodServiceImpl implements ISupplyGoodService{

    @Autowired
    private ISupplyGoodMapper mapper;

    @Value("${pageSize}")
    private Integer pageSize;

    @Override
    @Transactional
    public Integer newSupplyGood(SupplyGood supplyGood) {
        return mapper.insert(supplyGood);
    }

    @Override
    public SupplyGood findById(Integer id) {
        return mapper.findById(id);
    }

    @Override
    public PageInfo<SupplyGood> findBySupplierId(Integer pageNum, Integer supplierId) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<SupplyGood> pageInfo = new PageInfo<>(mapper.findBySupplierId(supplierId));
        return pageInfo;
    }

    @Override
    public PageInfo<SupplyGood> findSupplyGood(Integer pageNum, Integer type, String name, Integer supplierId) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<SupplyGood> pageInfo = null;
        if (type == null && Strings.isNullOrEmpty(name)){
            pageInfo = new PageInfo<SupplyGood>(mapper.findBySupplierId(supplierId));
        }else if (type == null){
            pageInfo = new PageInfo<SupplyGood>(mapper.findLikeName(name,supplierId));
        }else{
            pageInfo = new PageInfo<SupplyGood>(mapper.findByTypeAndName(type,name,supplierId));
        }
        return pageInfo;
    }


    @Override
    @Transactional
    public Integer update(SupplyGood supplyGood, Boolean isInfluential) {
        //获得要更新的供应商品记录
        Integer oldId = supplyGood.getId();
        SupplyGood targetGood = mapper.findById(oldId);
        if (targetGood.getNextRecordId() != null){
            throw new RelateException("该供应商品的记录与其他记录有联系，不是最新记录，不能更新！");
        }

        if (isInfluential == true){
            //确认供应商品的修改影响现有的采购计划和供应商品
            influence(oldId);
        }
        //插入新的记录
        Integer result = mapper.insert(supplyGood);
        //将新记录的id更新到旧记录中
        mapper.updateNextRecordId(oldId,supplyGood.getId());
        return result;
    }

    @Override
    @Transactional
    public Integer deleteById(Integer id) {
        //供应商品的删除影响现有的采购计划和供应商品
        influence(id);
        //将供应商品下一次更新记录的编号设置为0，间接表示删除商品
        Integer result = mapper.endById(id);
        return result;
    }

    //将含有供应商品的采购计划和订单进行过时和失效处理
    private Integer influence(Integer supplyGoodId){
        Integer purchasePlanObsolete = 0;
        Integer orderInvalid = 0;
        //供应商品的改动让采购计划过时
        List<Integer> purchasePlanIdList = mapper.findRelatePurchasePlanIdById(supplyGoodId);
        if (!purchasePlanIdList.isEmpty()){
            purchasePlanObsolete = mapper.makePurchasePlanObsoleteBatch(purchasePlanIdList);

        }
        //供应商品的改动让订单失效
        List<Integer> orderIdList = mapper.findRelateOrderById(supplyGoodId);
        if (!orderIdList.isEmpty()){
            orderInvalid = mapper.makeOrderInvalidBatch(orderIdList);
        }
        return purchasePlanObsolete + orderInvalid;
    }
}
